import dotenv from 'dotenv'
import path from 'path'

// 加载环境变量
dotenv.config({
  path: path.join(
    process.cwd(),
    process.env.NODE_ENV === 'production' ? '.env' : '.env.development'
  ),
})

interface EnvConfig {
  NODE_ENV: string
  PORT: number
  API_PREFIX: string
  CORS_ORIGIN: string
  PATHS: {
    LOGS: string
    UPLOADS: string
    TEMP: string
    BACKUPS: string
    FFMPEG: string
  }
  DATABASE: {
    MONGODB_URI: string
    MONGODB_USER?: string
    MONGODB_PASS?: string
    REDIS_HOST: string
    REDIS_PORT: number
    REDIS_PASS?: string
  }
  SERVICES: {
    SMS: {
      ACCESS_KEY?: string
      ACCESS_SECRET?: string
      SIGN_NAME: string
      TEMPLATE_CODE: string
    }
    STORAGE: {
      REGION: string
      SECRET_ID?: string
      SECRET_KEY?: string
      BUCKET?: string
    }
    CDN: {
      PUSH_DOMAIN?: string
      PLAY_DOMAIN?: string
      KEY?: string
    }
  }
  SECURITY: {
    JWT_SECRET: string
    CRYPTO_KEY: string
    ADMIN_EMAIL?: string
    ADMIN_PHONE?: string
    SSL: {
      KEY: string
      CERT: string
    }
  }
  DEV_TOOLS: {
    PROXY: {
      TARGET: string
      CHANGE_ORIGIN: boolean
    }
    DEBUG: {
      ENABLED: boolean
      LEVEL: string
    }
  }
}

export const envConfig: EnvConfig = {
  // 基础配置
  NODE_ENV: process.env.NODE_ENV || 'development',
  PORT: parseInt(process.env.PORT || '3000'),
  API_PREFIX: '/api',
  CORS_ORIGIN: process.env.CORS_ORIGIN || 'http://localhost:8080',

  // 文件路径配置(Windows格式)
  PATHS: {
    LOGS: 'D:\\livechat\\logs',
    UPLOADS: 'D:\\livechat\\uploads',
    TEMP: 'D:\\livechat\\temp',
    BACKUPS: 'D:\\livechat\\backups',
    FFMPEG: 'C:\\ffmpeg\\bin\\ffmpeg.exe',
  },

  // 数据库配置
  DATABASE: {
    MONGODB_URI: process.env.MONGODB_URI || 'mongodb://localhost:27017/livechat',
    MONGODB_USER: process.env.MONGODB_USER,
    MONGODB_PASS: process.env.MONGODB_PASS,
    REDIS_HOST: process.env.REDIS_HOST || 'localhost',
    REDIS_PORT: parseInt(process.env.REDIS_PORT || '6379'),
    REDIS_PASS: process.env.REDIS_PASSWORD,
  },

  // 第三方服务配置
  SERVICES: {
    // 短信服务(阿里云)
    SMS: {
      ACCESS_KEY: process.env.ALIYUN_ACCESS_KEY,
      ACCESS_SECRET: process.env.ALIYUN_ACCESS_SECRET,
      SIGN_NAME: '直播平台',
      TEMPLATE_CODE: 'SMS_123456789',
    },

    // 对象存储(腾讯云COS)
    STORAGE: {
      REGION: 'ap-guangzhou',
      SECRET_ID: process.env.COS_SECRET_ID,
      SECRET_KEY: process.env.COS_SECRET_KEY,
      BUCKET: process.env.COS_BUCKET,
    },

    // CDN配置(腾讯云)
    CDN: {
      PUSH_DOMAIN: process.env.LIVE_PUSH_DOMAIN,
      PLAY_DOMAIN: process.env.LIVE_PLAY_DOMAIN,
      KEY: process.env.LIVE_STREAM_KEY,
    },
  },

  // 安全配置
  SECURITY: {
    JWT_SECRET: process.env.JWT_SECRET || 'your-jwt-secret',
    CRYPTO_KEY: process.env.CRYPTO_KEY || 'your-crypto-key',
    ADMIN_EMAIL: process.env.ADMIN_EMAIL,
    ADMIN_PHONE: process.env.ADMIN_PHONE,
    // SSL证书路径(Windows格式)
    SSL: {
      KEY: 'D:\\certificates\\private.key',
      CERT: 'D:\\certificates\\certificate.crt',
    },
  },

  // 开发工具配置
  DEV_TOOLS: {
    // 开发环境代理
    PROXY: {
      TARGET: 'http://localhost:3000',
      CHANGE_ORIGIN: true,
    },
    // 调试配置
    DEBUG: {
      ENABLED: process.env.DEBUG === 'true',
      LEVEL: process.env.DEBUG_LEVEL || 'info',
    },
  },
}

// 验证必要的环境变量
const requiredEnvVars = ['MONGODB_URI', 'REDIS_HOST', 'JWT_SECRET']

for (const envVar of requiredEnvVars) {
  if (!process.env[envVar]) {
    console.warn(`警告: 环境变量 ${envVar} 未设置`)
  }
}

export default envConfig
